package com.cat.slidingWindow;

/**
 * @author cat
 * @description https://leetcode.cn/problems/number-of-substrings-containing-all-three-characters/
 * @create 2025/7/30 18:27
 * @since JDK17
 */

public class Solution15 {
    boolean judge(int[] nums) {
        for (int i = 0; i < 3; i++) {
            if (nums[i] <= 0) return false;
        }
        return true;
    }

    public int numberOfSubstrings(String str) {
        char[] s = str.toCharArray();
        int ans = 0, n = s.length;
        int[] cnt = new int[3];
        for (int l = 0, r = 0; r < n; l++) {
            while (r < n && !judge(cnt)) {  // 到第一个满足的位置，就停下来
                cnt[s[r] - 'a']++;  // 记录一个
            }
            if (judge(cnt)) {   // 如果满足
                ans += n - r + 1;
            }
            cnt[s[l] - 'a']--;  // 吐出一个
        }
        return ans;
    }
}
